package com.wm.picture.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wm.blog_common.bean.QueryPage;
import com.wm.blog_common.domain.TFileDO;
import com.wm.blog_common.entity.TFile;
import com.wm.blog_common.result.Result;
import com.wm.blog_config.util.QiNiuUtil;
import com.wm.picture.query.TFileQuery;
import com.wm.picture.service.TFileService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * <p>
 * 文件表 前端控制器
 * </p>
 *
 * @author wm_yu
 * @since 2020-03-26
 */
@Slf4j
@RestController
@RequestMapping("/web/picture")
public class TFileController {

    @Autowired
    private TFileService tFileService;

    /**
     * 根据ID查询
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result<TFileDO> get(@PathVariable Long id) {
        return tFileService.selectById(id);
    }

    /**
     * 根据ID删除
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public Result<Boolean> delete(@PathVariable Long id){
        return tFileService.deleteById(id);
    }

    /**
     * 文件上传到七牛云，并保存信息到mysql
     */
    @PostMapping(value = "/uploadFile")
    public Result uploadFile(@RequestPart("file") MultipartFile file){
        return tFileService.uploadFile(file);
    }

    /**
     * 测试文件上传到七牛云
     * @param file
     * @return
     */
    @PostMapping("/testUpload")
    public Result testUpload(MultipartFile file){
        return tFileService.testUpload(file);
    }

    /**
     * 修改
     * @param tFile
     * @return
     */
    @PutMapping
    public Result update(@RequestBody TFile tFile){
        return tFileService.update(tFile);
    }

    /**
     * 分页查询
     * @param current   查询页
     * @param size   每页显示条数
     * @param query 查询参数
     * @return
     */
    @GetMapping("/page")
    public Result<Page<TFileDO>> page(Integer current, Integer size, TFileQuery query){
        return tFileService.page(new QueryPage(current,size),query);
    }


    /**
     * 批量查询图片信息
     * @param idList
     * @return
     */
    @PostMapping("/getByIdList")
    public Result<List<TFile>> getByIdList(@RequestBody List<Long> idList){
        return tFileService.getByIdList(idList);
    }



}
